/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package activity1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author Thai Dac Vinh
 */
public class Activity1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try
        {
            String str="SELECT * FROM Authors WHERE city LIKE 'S%'";
            /*Initialize and load the Type 4 driver*/
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            /*Establish a connection with a data source*/
            Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=Library;user=sa;password=123");
            /*Create a Statement object to process the SELECT statement*/
            Statement stmt=con.createStatement();
            /*Execute the SELECT SQL statement*/
            ResultSet rs=stmt.executeQuery(str);
            System.out.println("Author ID\tAuthor Name\tCity");
            /*Display the result*/
            while (rs.next())
            {
                String id=rs.getString("au_id");
                String name=rs.getString("au_name");
                String city=rs.getString("city");
                System.out.print(id+"\t");
                /*Use tab to format the output.*/
                if (name.length() <= 7) {
                    System.out.print(name + "\t\t");
                } else {
                    System.out.print("\t" + name + "\t");
                }
                System.out.println(city);
            }
            con.close();
        }
        catch(Exception ex)
        {
            System.out.println("Error occurred");
            System.out.println("Error:" + ex);
        }
    }
}
